﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lsk.Data;
using FlickrNet;
using System.IO;

namespace Lsk.Business
{
    public class PhotoManager
    {
        private lskEntities _entity;

        /// <summary>
        /// This is a  API Key created for Flickr Account : lskproperty
        /// </summary>
        public const string ApiKey = "b6f35037be1bd1eab82c58bc69b01870";
        public const string SharedSecret = "a491935fa15e3872";

        delegate string GetResult(string result);
        public PhotoManager()
        {
            _entity = new lskEntities();
        }


        public IEnumerable<AW_Picture> GetListPhoto()
        {
            IEnumerable<AW_Picture> pictures = (from a in _entity.AW_Picture
                                                where a.PhotoId == null || a.pictureURL == null || a.PhotoId == "" || a.pictureURL == ""
                                               select a);
            return pictures;
        }


        public IEnumerable<AW_Staff> GetListPhotoStaff()
        {
            IEnumerable<AW_Staff> pictures = (from a in _entity.AW_Staff
                                                where a.PhotoId == null && a.pictureURL == null
                                                select a);
            return pictures;
        }

        public int Save(AW_Picture picture, PhotoInfo flickrInfo)
        {
            AW_Picture pictureSelected = (from a in _entity.AW_Picture where a.PictureId == picture.PictureId
                                             select a).FirstOrDefault();
            pictureSelected.PhotoId = flickrInfo.PhotoId;
            pictureSelected.MediumUrl = flickrInfo.MediumUrl;
            pictureSelected.pictureURL = flickrInfo.OriginalUrl;
            pictureSelected.SmallUrl = flickrInfo.SmallUrl;
            pictureSelected.LargeUrl = flickrInfo.LargeUrl;

            return _entity.SaveChanges();
            
        }


        public int Save(AW_Picture picture)
        {
            _entity.AW_Picture.Add(picture);
           return  _entity.SaveChanges();

        }

        public int Save(AW_Staff picture, PhotoInfo flickrInfo)
        {
            AW_Staff pictureSelected = (from a in _entity.AW_Staff
                                          where a.UserName == picture.UserName
                                          select a).FirstOrDefault();
            pictureSelected.PhotoId = flickrInfo.PhotoId;
            pictureSelected.MediumUrl = flickrInfo.MediumUrl;
            pictureSelected.pictureURL = flickrInfo.OriginalUrl;
            pictureSelected.SmallUrl = flickrInfo.SmallUrl;
            pictureSelected.LargeUrl = flickrInfo.LargeUrl;

            return _entity.SaveChanges();



        }

        public PhotoInfo Upload(Stream fileStream, AW_Picture picture)
        {
            PhotoInfo oPhotoInfo = new PhotoInfo();
            try
            {
                //Logger.log("Hello Test");
                Flickr flickr = new Flickr(ApiKey, SharedSecret);
                Flickr.CacheDisabled = true;
                flickr.HttpTimeout = 99999999;
            
                // Fix to Account lskproperty 
                flickr.OAuthAccessTokenSecret = "1f5050beaede7e58";
                flickr.OAuthAccessToken = "72157648688658473-07be80fa81fb1c32";
            
                string FileuploadedID = flickr.UploadPicture(fileStream, picture.Picture,picture.PropertyId.ToString(),picture.PictureDesc,picture.PictureId.ToString(), false, false, false, FlickrNet.ContentType.Photo, SafetyLevel.None, HiddenFromSearch.Hidden);
                oPhotoInfo = flickr.PhotosGetInfo(FileuploadedID);
             
           
            }
             catch (Exception ex)
            {
                Logger.log("Flickr upload error : " +ex.Message);
                 
                throw new Exception("Flickr upload error! Please retry. Exception:" + ex);
            }
            return oPhotoInfo;

        }   


        public PhotoInfo UploadStaff(Stream fileStream, AW_Staff staff)
        {
            PhotoInfo oPhotoInfo = new PhotoInfo();
            //try
            {
                Flickr flickr = new Flickr(ApiKey, SharedSecret);
                flickr.HttpTimeout = 99999999;

                // Fix to Account lskproperty 
                flickr.OAuthAccessTokenSecret = "1f5050beaede7e58";
                flickr.OAuthAccessToken = "72157648688658473-07be80fa81fb1c32";

                string FileuploadedID = flickr.UploadPicture(fileStream, staff.StaffId.ToString(), staff.UserName, staff.Email, staff.Avatar, false, false, false, FlickrNet.ContentType.Photo, SafetyLevel.None, HiddenFromSearch.Hidden);
                oPhotoInfo = flickr.PhotosGetInfo(FileuploadedID);


            }
            //catch (Exception ex)
            {
            //    Logger.log("Flickr upload error : " + ex.Message);

                //throw new Exception("Flickr upload error! Please retry");
            }
            return oPhotoInfo;




        }

        public void UploadAsync(Stream fileStream, AW_Picture picture)
        {
            Flickr flickr = new Flickr(ApiKey, SharedSecret);
            flickr.HttpTimeout = 99999999;
            // Fix to Account lskproperty 
            flickr.OAuthAccessTokenSecret = "1f5050beaede7e58";
            flickr.OAuthAccessToken = "72157648688658473-07be80fa81fb1c32";
            Action<FlickrResult<string>> resultFlickr = new Action<FlickrResult<string>>(SaveCallback);
            
            flickr.UploadPictureAsync(fileStream, picture.Picture, picture.PropertyId.ToString(), picture.PictureDesc, picture.PictureId.ToString(), false, false, false, FlickrNet.ContentType.Photo, SafetyLevel.None, HiddenFromSearch.Hidden, resultFlickr);
           

        }

        private void SaveCallback(FlickrResult<string> obj)
        {
            Flickr flickr = new Flickr(ApiKey, SharedSecret);
            // Fix to Account lskproperty 
            flickr.OAuthAccessTokenSecret = "1f5050beaede7e58";
            flickr.OAuthAccessToken = "72157648688658473-07be80fa81fb1c32";

            PhotoInfo oPhotoInfo = flickr.PhotosGetInfo(obj.Result);
            Guid pictureId = new Guid(oPhotoInfo.Tags[0].TagText);
            AW_Picture photo = (from a in _entity.AW_Picture where a.PictureId ==pictureId select a).FirstOrDefault();
            Save(photo, oPhotoInfo);
        }

      
    }
}
    